ALTER PROCEDURE sp_save_unit_cost_details (
@MRKT_PART_NO   VARCHAr(50),
@costAccount    VARCHAR(50),
@VaDie			NUMERIC(18,4),
@VaEProm		NUMERIC(18,4),
@VaBG			NUMERIC(18,4),
@VaDieFrt		NUMERIC(18,4),
@VaAssy			NUMERIC(18,4),
@VaAssyFrt		NUMERIC(18,4),
@VaTest			NUMERIC(18,4),
@VaTR			NUMERIC(18,4),
@StdInAssy		NUMERIC(18,4),
@StdInTest		NUMERIC(18,4),
@StdInTR		NUMERIC(18,4),
@StdOutAssy		NUMERIC(18,4),
@StdOutTest		NUMERIC(18,4),
@StdOutTR		NUMERIC(18,4),
@YieldAssy		NUMERIC(18,4),
@YieldTest		NUMERIC(18,4),
@YieldTR		NUMERIC(18,4),
@NextQtr		NUMERIC(18,4),
@NextQtrPlusOne	NUMERIC(18,4)
)

AS
BEGIN
		DECLARE @PROCESSSTEP_ID INT
		DECLARE @START_DATE 	DATETIME
    	DECLARE @END_DATE 	DATETIME
    	DECLARE @TEMP_VARCHAR 	VARCHAR(20)
    	    
   		SET @TEMP_VARCHAR = CONVERT(VARCHAR, CURRENT_TIMESTAMP, 101)+' 12:00AM'
    	SET @START_DATE = CONVERT(DATETIME, @TEMP_VARCHAR)
    	SET @TEMP_VARCHAR = NULL
    	SET @TEMP_VARCHAR = CONVERT(VARCHAR, CURRENT_TIMESTAMP, 101)+' 11:59PM' 
    	SET @END_DATE = CONVERT(DATETIME, @TEMP_VARCHAR)

		SELECT TOP 1 @PROCESSSTEP_ID = PROCESSSTEP_ID FROM PRODUCT WHERE P_MARKETING_PART_NUMBER = @MRKT_PART_NO
		IF ( @PROCESSSTEP_ID IS NULL OR @PROCESSSTEP_ID <= 0 )
		BEGIN
			RETURN
		END 
					
		INSERT INTO UNIT_COST_TABLE ( [Processstep_Id],[Cost_Account],[Va_Die],[Va_Eprom],[Va_Bg],[Va_DieFrt],[Va_Assy],[Va_AssyFrt],[Va_Test],[Va_Tr],
			[StdIn_Assy],[StdIn_Test],[StdIn_Tr],[StdOut_Assy],[StdOut_Test],[StdOut_Tr],[Yield_Assy],[Yield_Test],[Yield_Tr],
		[Std_NextQtr_Cost],[Std_NextQtrPlusOne_Cost],[Timestamp] ) 
		 VALUES ( @PROCESSSTEP_ID,@costAccount,@VaDie,@VaEProm,@VaBG,@VaDieFrt,@VaAssy,@VaAssyFrt,@VaTest,@VaTR,
			@StdInAssy,@StdInTest,@StdInTR,@StdOutAssy,@StdOutTest,@StdOutTR,@YieldAssy,@YieldTest,@YieldTR,
			@NextQtr,@NextQtrPlusOne, CURRENT_TIMESTAMP) 

		
		INSERT INTO UNIT_COST_HISTORY_TABLE ( [Processstep_Id],[Cost_Account],[Va_Die],[Va_Eprom],[Va_Bg],[Va_DieFrt],[Va_Assy],[Va_AssyFrt],[Va_Test],[Va_Tr],
			[StdIn_Assy],[StdIn_Test],[StdIn_Tr],[StdOut_Assy],[StdOut_Test],[StdOut_Tr],[Yield_Assy],[Yield_Test],[Yield_Tr],
		[Std_NextQtr_Cost],[Std_NextQtrPlusOne_Cost],[Timestamp] ) 
		 VALUES ( @PROCESSSTEP_ID,@costAccount,@VaDie,@VaEProm,@VaBG,@VaDieFrt,@VaAssy,@VaAssyFrt,@VaTest,@VaTR,
			@StdInAssy,@StdInTest,@StdInTR,@StdOutAssy,@StdOutTest,@StdOutTR,@YieldAssy,@YieldTest,@YieldTR,
			@NextQtr,@NextQtrPlusOne, CURRENT_TIMESTAMP)
		
END


